package com.shujia.day16.tcp.tcp2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        /*
            TODO
                1.从控制台不断接收数据 => 将接收的数据发送给服务端
         */
        Scanner scanner = new Scanner(System.in);
        Socket socket = new Socket("localhost", 9999);
        OutputStream outputStream = socket.getOutputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String readLine = "";
        System.out.println("请输入您要发送的信息：");
        while (scanner.hasNext()) {
            String message = scanner.nextLine();
            outputStream.write((message+"\n").getBytes());
            outputStream.flush();
            while ((readLine = bufferedReader.readLine()) != null) {
                System.out.println("服务端:"+readLine);
            }
        }
        outputStream.close();
        socket.close();
    }
}
